#ifndef _MB_PDU_H_
#define _MB_PDU_H_

#include "stdint.h"

#define MB_MAX_PDU_DATA_LEN 252

typedef enum 
{
  MB_EXP_NONE,
  MB_EXCEPTION_FUNCODE_1,        // 功能码不支持
  MB_EXCEPTION_FUNCODE_2,        // 寄存器的信息错误 
  MB_EXCEPTION_FUNCODE_3,        // 输出数量大于 0x07D0   2000
  MB_EXCEPTION_FUNCODE_4         // 读离散输出
} MB_COILS_EXCEPTION_CODE;


#pragma pack(1)

/**
 * @brief 错误响应PDU
*/
typedef struct __MB_ExceptionPDU_HandleTypeDef
{
    uint8_t FunctionCode;         //原功能码 | 0x80
    uint8_t ExceptionCode;        //错误代码 4,3,2,1
}MB_ExceptionPDU_HandleTypeDef;


/**
 * @brief 通用PDU
 */
typedef struct __MB_PDU_HandleTypeDef
{
    uint8_t  FunctionCode;
		uint8_t  Data[MB_MAX_PDU_DATA_LEN];
}MB_PDU_HandleTypeDef;

/**
 * @brief 读请求PDU
 */
typedef struct __MB_READ_REQPDU_HandleTypeDef
{
    uint8_t  FunctionCode;
    uint16_t StartAddress;
    uint16_t Quantity;
}MB_READ_REQPDU_HandleTypeDef;

/**
 * @brief 读响应PDU
*/
typedef struct __MB_READ_RSPPDU_HandleTypeDef
{
    uint8_t  FunctionCode;
    uint8_t  ByteCount;
    uint8_t  Data[MB_MAX_PDU_DATA_LEN];
}MB_READ_RSPPDU_HandleTypeDef;

/**
 * @brief 写单个PDU 请求
 */
typedef struct __MB_WRITE_SINGLE_REQPDU_HandleTypeDef
{
    uint8_t  FunctionCode;
    uint16_t StartAddress;
    uint16_t Value;
}MB_WRITE_SINGLE_REQPDU_HandleTypeDef;

/**
 * @brief 写单个PDU 响应
 */
typedef struct __MB_WRITE_SINGLE_RSPPDU_HandleTypeDef
{
    uint8_t  FunctionCode;
    uint16_t StartAddress;
    uint16_t Value;
}MB_WRITE_SINGLE_RSPPDU_HandleTypeDef;

/**
 * @brief 写多个 PDU 请求
 */
typedef struct __MB_WRITE_MULTIPLE_REQPDU_HandleTypeDef
{
    uint8_t  FunctionCode;
    uint16_t StartAddress;
    uint16_t Quantity;
    uint8_t  ByteCount;
    uint8_t  Data[MB_MAX_PDU_DATA_LEN];
}MB_WRITE_MULTIPLE_REQPDU_HandleTypeDef;

/**
 * @brief 写多个 PDU 请求
 */
typedef struct __MB_WRITE_MULTIPLE_RSPPDU_HandleTypeDef
{
    uint8_t  FunctionCode;
    uint16_t StartAddress;
    uint16_t Quantity;
}MB_WRITE_MULTIPLE_RSPPDU_HandleTypeDef;

/**
 * @brief 读写多个 PDU 请求
 */
typedef struct __MB_READWRITE_MULTIPLE_REQPDU_HandleTypeDef
{
    uint8_t  FunctionCode;
    uint16_t ReadStartAddress;
    uint16_t ReadQuantity;
    uint16_t WriteStartAddress;
    uint16_t WriteQuantity;
    uint8_t  WriteByteCount;
    uint8_t  Data[MB_MAX_PDU_DATA_LEN];
}MB_READWRITE_MULTIPLE_REQPDU_HandleTypeDef;

#pragma pack(4)

#endif

